package com.mes.web.controller.lf;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.hy.Equipment;
import com.mes.entity.hy.PeopleManage;
import com.mes.entity.hy.Product;
import com.mes.service.lf.DeviceService;
import com.mes.utils.ResponseUtil;

/**
 * 设备的controller层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/device")
public class DeviceController {
	
	@Resource
	DeviceService deviceService;
	

	/**
	 * 查询所有设备编号（显示在下拉框）
	 * @param response
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/allDevice")
	public void allDevice(HttpServletResponse response) throws Exception{
		List<Equipment> lists = deviceService.findAllDevice();
		JSONArray fromObject = JSONArray.fromObject(lists);
		ResponseUtil.write(response, fromObject);
	}
	
	@ResponseBody
	@RequestMapping("/findProByDeviceId")
	public void findProByDeviceId(HttpServletResponse response, Long id) throws Exception{
		//根据设备的id查询车种、模具编号
		Equipment equipment = deviceService.findByDeviceId(id);
		//根据车种、模具编号、设备id查询出产品的List
		List<Product> lists = deviceService.findProductOfDev(equipment);
		JSONArray fromObject = JSONArray.fromObject(lists);
		ResponseUtil.write(response, fromObject);
	}
	
	/**
	 * 根据选中的设备编号查询模具编号以及车种
	 * @param response
	 * @param id
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findByDeviceId")
	public void findByDeviceId(HttpServletResponse response, Long id, String name1, String name2) throws Exception{
		//根据设备的id查询车种、模具编号
		Equipment equipment = deviceService.findByDeviceId(id);
		//根据车种、模具编号、设备id查询出产品的List
		List<Product> lists = deviceService.findProductOfDev(equipment);
		//查询计划量
		//ArrayList<String> nums = new ArrayList<String>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		PeopleManage pm = new PeopleManage();
		for (int i = 0; i < lists.size(); i++) {
			//根据设备型号查询设备名称
			String devName = deviceService.findDevNameByDevVersion(lists.get(i).getRemark());
			/*map.put("product", lists.get(i));*/
			//封装产品编号，设备名称，当前时间，班组
			map.put("productId", lists.get(i).getPid());
			map.put("device", devName);
			map.put("time", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			map.put("class", "2");
			
			map.put("pId", lists.get(i).getNum());
			map.put("deviceId", id);
			//查询计划量
			String planNum = deviceService.findPlanNumByCondition(map);
			//查询不良品数量
			//int nogoodNum = deviceService.findNoGoodNum(map);
			if(planNum != null || !"".equals(planNum)){
				//nums.set(i, planNum);
				lists.get(i).setRemark9(planNum);
			}
			/*if(nogoodNum >= 0){
				lists.get(i).setRemark8(nogoodNum + "");
			}*/
			/*String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			pm.setNdate(time);
			pm.setStarttime(time);
			pm.setMachin(devName);
			pm.setPnum(lists.get(i).getNum());
			pm.setPname(lists.get(i).getName());
			pm.setStartnum("0");
			pm.setStartgood("0");
			pm.setStartngood("0");
			pm.setWorkstate("作业中");
			
			deviceService.addPeopleManage(pm);*/
		}
		//System.out.println(lists);
		
		//点击“作业开始”，将页面信息保存到jy_pm
		/*deviceService.saveToPeopleManager();*/
		
		JSONArray fromObject = JSONArray.fromObject(lists);
		ResponseUtil.write(response, fromObject);
	}
	
	/**
	 * 根据员工工号查询班组
	 * @param workid1
	 * @param workid2
	 */
	public void fingClassByUser(String workid1,String workid2){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("workid1", workid1);
		map.put("workid2", workid2);
		/*deviceService.fingClassByUser(map);*/
	}
	
	
	/*public void findPlanNumByCondition(){
		
	}*/
	
	 
}
